package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.*;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.mapper.OrderDetailMapper;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.AddressService;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    //购物车信息
    @Autowired
    private CartService cartService;

    //地址信息
    @Autowired
    private AddressMapper addressMapper;

    //订单
    @Autowired
    private OrderMapper orderMapper;

    //订单明细
    @Autowired
    private OrderDetailMapper orderDetailMapper;


    //用户下单
    @Override
    public void submit(Order order) {
        //用户信息不能为空
        User user = UserHolder.get();
        if (user == null) {
            throw new CustomException("用户信息为空,不可下单");
        }

        //地址信息不能为空
        Address address = addressMapper.selectById(order.getAddressId());
        if (address == null) {
            throw new CustomException("地址信息为空,不可下单");
        }

        //购物车不能为空
        List<Cart> cartList = cartService.list();
        if (CollUtil.isEmpty(cartList)) {
            throw new CustomException("购物车为空,不可下单");
        }

        //总金额计算
        BigDecimal amount = new BigDecimal("0");
        //生成订单详情id
        Long orderId = IdWorker.getId();

        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setName(cart.getName());//名称
            orderDetail.setOrderId(orderId);//订单ID
            orderDetail.setDishId(cart.getDishId());//菜品ID
            orderDetail.setSetmealId(cart.getSetmealId());//套餐ID
            orderDetail.setDishFlavor(cart.getDishFlavor());//口味
            orderDetail.setNumber(cart.getNumber());//数量
            orderDetail.setImage(cart.getImage());//图片
            orderDetail.setAmount(cart.getAmount());//菜品单价
            amount = amount.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber())));//总金额
            orderDetailMapper.insert(orderDetail);
        }
        //添加订单信息
        order.setId(orderId);
        order.setNumber(orderId.toString());
        order.setStatus(1);
        order.setUserId(user.getId());
        order.setAddress(address.getDetail());
        order.setOrderTime(new Date());
        order.setCheckoutTime(new Date());
        order.setAmount(amount);
        order.setRemark(order.getRemark());//备注
        order.setUserName(user.getName());//用户名字
        order.setPhone(user.getPhone());//手机号
        order.setAddress(address.getDetail());//地址
        order.setConsignee(address.getConsignee());//收货人
        orderMapper.insert(order);
        //清空购物车
        cartService.delete();

    }


    //分页查找订单信息-客户端
    @Override
    public Page<Order> findPage(Integer pageNum, Integer pageSize) {
        Page<Order> pageOrder = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.eq(Order::getUserId, UserHolder.get().getId());
        qw.orderByDesc(Order::getOrderTime);
        pageOrder = orderMapper.selectPage(pageOrder, qw);

        for (Order order : pageOrder.getRecords()) {
            LambdaQueryWrapper<OrderDetail> qw2 = new LambdaQueryWrapper<>();
            qw2.eq(OrderDetail::getOrderId, order.getId());
            List<OrderDetail> orderDetails = orderDetailMapper.selectList(qw2);
            order.setOrderDetails(orderDetails);
        }
        return pageOrder;
    }

    //分页查找订单信息-服务器
    @Override
    public Page <Order> orderPage(Integer pageNum, Integer pageSize, Date beginTime, Date endTime,String number) {
        Page<Order> orderPage = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Order> qw = new LambdaQueryWrapper<>();
        qw.between(beginTime!=null || endTime!=null,Order::getOrderTime,beginTime,endTime);
        qw.eq(number!=null,Order::getNumber,number);//如果订单号不为空
        orderPage = orderMapper.selectPage(orderPage, qw);

        return orderPage;
    }


}
